from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, String, Time, Date
from database.models.DatabaseCommon import Base
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from database.models.ControllerType import ControllerType
from database.models.Controller import Controller
from database.models.ControllerPoint import ControllerPoint
from database.models.Normal import Normal
from database.models.OptType import OptType


class TimingSwitch(Base):
    __tablename__ = 'TimingSwitch'
    id = Column(Integer, primary_key=True, autoincrement=True)
    desc = Column(String, unique=True)
    IsRunning = Column(Integer, default=0)
    enable = Column(Integer, default=0)
    OptTypeID = Column(Integer, ForeignKey('OptType.id'))
    opttype = relationship('OptType', back_populates='timingswitch')
    Params = Column(String)
    PointID = Column(Integer, ForeignKey('ControllerPoints.id'))
    point = relationship('ControllerPoint', back_populates='timingswitch')
    wallTime = Column(Integer, default=0)
    everyDay = Column(String, default='')
    weekDay = Column(String, default='')
    sunup_lightoff = Column(Integer, default=0)
    sunup_lightoff_delay = Column(Integer, default=0)
    sunup_lighton = Column(Integer, default=0)
    sunup_lighton_delay = Column(Integer, default=0)
    sundown_lightoff = Column(Integer, default=0)
    sundown_lightoff_delay = Column(Integer, default=0)
    sundown_lighton = Column(Integer, default=0)
    sundown_lighton_delay = Column(Integer, default=0)

    def __repr__(self):
        return "<TimingSwitch (id='%d', desc='%s')>" % (
            self.id, self.desc)


OptType.timingswitch = relationship('TimingSwitch', back_populates='opttype')
ControllerPoint.timingswitch = relationship('TimingSwitch', back_populates='point')
